//
//  YHEmotionBarView.swift
//  XLWB
//
//  Created by key on 16/7/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let buttonNum = 1000 //button的tag值 添加缓冲值 避免和其他View 的tag冲突

enum YHEmotionBarViewType: Int {
    
    //默认
    case Normal = 0
    
    //Emoji
    case Emoji = 1
    
    //浪小花
    case Lxh = 2
}


class YHEmotionBarView: UIView {

    //中间记录点击Button
    var currentButton: UIButton?
    
    //MARK: - 入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //加载子控件
        addChildView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 加载子控件
    func addChildView() {
        
        //添加
        let normal = setButtonBar("left", title: "默认", type: YHEmotionBarViewType.Normal)
        
        let emoji = setButtonBar("mid", title: "Emoji", type: YHEmotionBarViewType.Emoji)
        
        let lxh = setButtonBar("right", title: "浪小花", type: YHEmotionBarViewType.Lxh)
        
        
        //约束
        normal.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(emoji)
        }
        
        emoji.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(normal.snp_trailing)
            make.width.equalTo(lxh)
        }
        
        lxh.snp_makeConstraints { (make) -> Void in
            
            make.bottom.top.trailing.equalTo(self)
            make.leading.equalTo(emoji.snp_trailing)
        }
        
        
    }

    //创建子控件公共方法
    func setButtonBar(imageName: String, title: String, type: YHEmotionBarViewType) -> UIButton {
        
        /*
            compose_emotion_table_left_normal
            compose_emotion_table_left_selected
        */
        let button: UIButton = UIButton()
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        addSubview(button)
        
        
        button.tag = type.rawValue + buttonNum
        
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        //设置默认状态为出示选中状态
        if type == .Normal {
            
            button.selected = true
            button.enabled = false
            
            currentButton = button
        }
        
        
        return button
        
    }
    
    
    //点击button 响应事件
    //声明闭包
    var emotionBarClosure: ((type: YHEmotionBarViewType) -> ())?
    func buttonClick(button: UIButton) {
        
        //取消上一个button选中状态 开启交互
        currentButton?.selected = false
        currentButton?.enabled = true
        
        //设置当前点击button的选中状态 关闭交互
        button.selected = true
        button.enabled = false
        
        //把当前button 赋值给第三方
        currentButton = button
        
        
        
        //调用闭包 实现点击事件
        emotionBarClosure?(type:YHEmotionBarViewType(rawValue: button.tag - buttonNum)!)
        
        
    }
    
    
    
    //MARK: - 定义方法 供外界调用 实现button按钮 和 上方 表情图片的滚动之间的交互
    func setSelectedButton(tag: Int){
        
        //根据tag值 获取对应button
        let button = viewWithTag(tag + buttonNum) as! UIButton  
        
        if button == currentButton {
            
            return
        }
        
        //取消上一个button选中状态 开启交互
        currentButton?.selected = false
        currentButton?.enabled = true
        
        //设置当前点击button的选中状态 关闭交互
        button.selected = true
        button.enabled = false
        
        //把当前button 赋值给第三方
        currentButton = button
        
        
    }
    
}








